package com.manage.system.svc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.ContextHolderUtils;
import com.manage.common.core.utils.PageUtil;
import com.manage.common.core.utils.StringUtils;
import com.manage.system.api.entity.SysDictType;
import com.manage.system.api.request.dict.DictTypeAddOrEditParam;
import com.manage.system.api.request.dict.DictTypeQueryParam;
import com.manage.system.svc.mapper.SysDictTypeMapper;
import com.manage.system.api.service.ISysDictTypeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-11-23
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    private final SysDictTypeMapper sysDictTypeMapper;

    @Override
    public JsonResult findDictTypeListPage(DictTypeQueryParam dictTypeQueryParam) {
        LambdaQueryWrapper<SysDictType> lambda = new QueryWrapper<SysDictType>().lambda()
                .eq(!StringUtils.isEmpty(dictTypeQueryParam.getDictName()), SysDictType::getDictName,dictTypeQueryParam.getDictName())
                .eq(!StringUtils.isEmpty(dictTypeQueryParam.getDictType()), SysDictType::getDictType,dictTypeQueryParam.getDictType())
                .eq(!StringUtils.isNull(dictTypeQueryParam.getStatus()),SysDictType::getStatus,dictTypeQueryParam.getStatus());

        if(!StringUtils.isEmpty(dictTypeQueryParam.getBeginTime())&&!StringUtils.isEmpty(dictTypeQueryParam.getEndTime())){
            lambda.ge(SysDictType::getCreateTime,dictTypeQueryParam.getBeginTime())
                    .le(SysDictType::getCreateTime,dictTypeQueryParam.getEndTime());
        }
        PageUtil.startPageSort(dictTypeQueryParam.getPageNum(), dictTypeQueryParam.getPageSize(), dictTypeQueryParam.getSortField(), dictTypeQueryParam.getSortOrder());
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectList(lambda);
        return JsonResult.success(PageUtil.restPage(sysDictTypes));
    }

    @Override
    public JsonResult batchDelDictType(List<Long> ids) {
        log.info("系统服务->字典管理->批量删除字典类型->参数: ids:{}",ids.toString());
        return JsonResult.toJsonResult(sysDictTypeMapper.deleteBatchIds(ids));
    }

    @Override
    public JsonResult addDictType(DictTypeAddOrEditParam dictTypeAddOrEditParam) {
        log.info("系统服务->字典管理->添加字典类型->参数: {}",dictTypeAddOrEditParam.toString());
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dictTypeAddOrEditParam,sysDictType);
        sysDictType.setCreateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysDictTypeMapper.insert(sysDictType));
    }

    @Override
    public JsonResult editDictType(DictTypeAddOrEditParam dictTypeAddOrEditParam) {
        log.info("系统服务->字典管理->修改字典类型->参数: {}",dictTypeAddOrEditParam.toString());
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dictTypeAddOrEditParam,sysDictType);
        sysDictType.setUpdateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysDictTypeMapper.updateById(sysDictType));
    }
}
